<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="自增键oid" prop="oid">
      <el-input v-model="dataForm.oid" placeholder="自增键oid"></el-input>
    </el-form-item>
    <el-form-item label="字典名称" prop="dictName">
      <el-input v-model="dataForm.dictName" placeholder="字典名称"></el-input>
    </el-form-item>
    <el-form-item label="字典类型" prop="dictType">
      <el-input v-model="dataForm.dictType" placeholder="字典类型"></el-input>
    </el-form-item>
    <el-form-item label="创建人UID" prop="createByUid">
      <el-input v-model="dataForm.createByUid" placeholder="创建人UID"></el-input>
    </el-form-item>
    <el-form-item label="最后更新人UID" prop="updateByUid">
      <el-input v-model="dataForm.updateByUid" placeholder="最后更新人UID"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="是否发布(1:是，0:否)" prop="isPublish">
      <el-input v-model="dataForm.isPublish" placeholder="是否发布(1:是，0:否)"></el-input>
    </el-form-item>
    <el-form-item label="排序字段" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序字段"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          oid: '',
          dictName: '',
          dictType: '',
          createByUid: '',
          updateByUid: '',
          remark: '',
          status: '',
          createTime: '',
          updateTime: '',
          isPublish: '',
          sort: ''
        },
        dataRule: {
          oid: [
            { required: true, message: '自增键oid不能为空', trigger: 'blur' }
          ],
          dictName: [
            { required: true, message: '字典名称不能为空', trigger: 'blur' }
          ],
          dictType: [
            { required: true, message: '字典类型不能为空', trigger: 'blur' }
          ],
          createByUid: [
            { required: true, message: '创建人UID不能为空', trigger: 'blur' }
          ],
          updateByUid: [
            { required: true, message: '最后更新人UID不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          isPublish: [
            { required: true, message: '是否发布(1:是，0:否)不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序字段不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/user/sysdicttype/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.oid = data.sysDictType.oid
                this.dataForm.dictName = data.sysDictType.dictName
                this.dataForm.dictType = data.sysDictType.dictType
                this.dataForm.createByUid = data.sysDictType.createByUid
                this.dataForm.updateByUid = data.sysDictType.updateByUid
                this.dataForm.remark = data.sysDictType.remark
                this.dataForm.status = data.sysDictType.status
                this.dataForm.createTime = data.sysDictType.createTime
                this.dataForm.updateTime = data.sysDictType.updateTime
                this.dataForm.isPublish = data.sysDictType.isPublish
                this.dataForm.sort = data.sysDictType.sort
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/sysdicttype/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'oid': this.dataForm.oid,
                'dictName': this.dataForm.dictName,
                'dictType': this.dataForm.dictType,
                'createByUid': this.dataForm.createByUid,
                'updateByUid': this.dataForm.updateByUid,
                'remark': this.dataForm.remark,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'isPublish': this.dataForm.isPublish,
                'sort': this.dataForm.sort
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
